package com.event.management.frontend.mapper;

import com.event.management.frontend.domain.Users;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Options;

/**
 * 用户Mapper接口
 */
@Mapper
public interface UsersMapper {
    
    /**
     * 根据用户名查询用户
     * @param username 用户名
     * @return 用户对象
     */
    @Select("SELECT * FROM users WHERE username = #{username}")
    Users selectByUsername(@Param("username") String username);
    
    /**
     * 根据ID查询用户
     * @param id 用户ID
     * @return 用户对象
     */
    @Select("SELECT * FROM users WHERE id = #{id}")
    Users selectById(@Param("id") Integer id);
    
    /**
     * 更新用户信息
     * @param user 用户对象
     * @return 影响行数
     */
    @Update("UPDATE users SET updated_at = #{updatedAt} WHERE id = #{id}")
    int updateById(Users user);
    
    /**
     * 完整更新用户信息
     * @param user 用户对象
     * @return 影响行数
     */
    @Update("<script>"
            + "UPDATE users SET "
            + "<if test='realName != null'>real_name = #{realName}, </if>"
            + "<if test='gender != null'>gender = #{gender}, </if>"
            + "<if test='email != null'>email = #{email}, </if>"
            + "<if test='phone != null'>phone = #{phone}, </if>"
            + "<if test='interests != null'>interests = #{interests}, </if>"
            + "<if test='birthday != null'>birthday = #{birthday}, </if>"
            + "<if test='bio != null'>bio = #{bio}, </if>"
            + "<if test='avatar != null'>avatar = #{avatar}, </if>"
            + "<if test='address != null'>address = #{address}, </if>"
            + "<if test='profession != null'>profession = #{profession}, </if>"
            + "<if test='password != null'>password = #{password}, </if>"
            + "updated_at = #{updatedAt} "
            + "WHERE id = #{id}"
            + "</script>")
    int updateUserInfo(Users user);
    
    /**
     * 插入新用户
     * @param user 用户对象
     * @return 影响行数
     */
    @Insert("INSERT INTO users (username, password, real_name, gender, email, phone, interests, birthday, bio, avatar, address, profession, role, created_at, updated_at) " +
            "VALUES (#{username}, #{password}, #{realName}, #{gender}, #{email}, #{phone}, #{interests}, #{birthday}, #{bio}, #{avatar}, #{address}, #{profession}, #{role}, #{createdAt}, #{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Users user);
} 